<template>
    <el-dialog :title="!dataForm.pkSecret ? '新增' : '修改'" destroy-on-close :close-on-click-modal="false"
        :before-close="closeDailog" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="密钥类型" prop="secretType">
                <ec-dict-select v-model="dataForm.secretType" placeholder="请选择密钥类型" dictType="secret_type">
                </ec-dict-select>
            </el-form-item>
            <el-form-item label="密钥描述" prop="secretDesc">
                <el-input v-model="dataForm.secretDesc" placeholder="密钥描述"></el-input>
            </el-form-item>
            <el-form-item label="appId" prop="appId">
                <el-input v-model="dataForm.appId" placeholder="appId"></el-input>
            </el-form-item>
            <el-form-item label="appSecret" prop="appSecret">
                <el-input v-model="dataForm.appSecret" placeholder="appSecret"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as secretService from '@/api/bdms/secret'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    revision: '',
                    pkSecret: 0,
                    secretType: '',
                    secretDesc: '',
                    appId: '',
                    appSecret: ''
                },
                dataRule: {
                    secretType: [{
                        required: true,
                        message: '密钥类型不能为空',
                        trigger: 'blur'
                    }],
                    secretDesc: [{
                        required: true,
                        message: '密钥描述不能为空',
                        trigger: 'blur'
                    }],
                    appId: [{
                        required: true,
                        message: 'appId不能为空',
                        trigger: 'blur'
                    }],
                    appSecret: [{
                        required: true,
                        message: 'appSecret不能为空',
                        trigger: 'blur'
                    }]
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkSecret = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkSecret) {
                        secretService.getsecretinfo(this.dataForm.pkSecret).then(data => {
                            this.dataForm.revision = data.revision
                            this.dataForm.secretType = data.secretType
                            this.dataForm.secretDesc = data.secretDesc
                            this.dataForm.appId = data.appId
                            this.dataForm.appSecret = data.appSecret
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'revision': this.dataForm.revision,
                            'pkSecret': this.dataForm.pkSecret || undefined,
                            'secretType': this.dataForm.secretType,
                            'secretDesc': this.dataForm.secretDesc,
                            'appId': this.dataForm.appId,
                            'appSecret': this.dataForm.appSecret
                        }
                        var tick = !this.dataForm.pkSecret ? secretService.addsecret(params) : secretService
                            .updatesecret(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>